package com.codeyang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codeyang.constans.Constants;
import com.codeyang.domain.Dept;
import com.codeyang.dto.DeptDto;
import com.codeyang.mapper.DeptMapper;
import com.codeyang.service.DeptService;
import com.codeyang.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 描述: TO-删除标志-DO
 *
 * @author CodeYang_Site
 * @version 2021/4/20 15:34
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {



    @Autowired
    private DeptMapper deptMapper;


    @Override
    public DataGridView lisPage(DeptDto deptDto) {
        // 分页插件+排序
        IPage<Dept> page = new Page<>(deptDto.getPageNum(), deptDto.getPageSize());
        page.orders().add(OrderItem.desc("update_time"));
        QueryWrapper<Dept> qw = new QueryWrapper<>();
        //条件
        qw.like(StringUtils.isNotBlank(deptDto.getDeptName()), Dept.COL_DEPT_NAME,deptDto.getDeptName());
        qw.eq(StringUtils.isNotBlank(deptDto.getStatus()), Dept.COL_STATUS,deptDto.getStatus());
        qw.between(deptDto.getBeginTime()!=null && deptDto.getEndTime()!=null , Dept.COL_CREATE_TIME,deptDto.getBeginTime(),deptDto.getEndTime());
        deptMapper.selectPage(page, qw);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    @Override
    public boolean saveDept(DeptDto deptDto) {
        Dept dept = new Dept();
        BeanUtils.copyProperties(deptDto, dept);
        // 处理新增
        dept.setCreateTime(new Date());
        dept.setCreateBy(deptDto.getSimpleUser().getUserName());
        //处理修改
        dept.setUpdateTime(dept.getCreateTime());
        return deptMapper.insert(dept) > 0;
    }

    @Override
    public boolean updateDept(DeptDto deptDto) {
        Dept dept = new Dept();
        BeanUtils.copyProperties(deptDto, dept);
        //处理修改
        dept.setUpdateTime(new Date());
        dept.setUpdateBy(deptDto.getSimpleUser().getUserName());
        return deptMapper.updateById(dept) > 0;
    }

    /**
     * 全查询部门
     * --- 在用户管理调用全查询
     * --- 其他服务
     * @return
     */
    @Override
    public List<Dept> selectAllDept() {

        QueryWrapper<Dept> qw = new QueryWrapper<>();
        // 状态 只查询启用了的部门的状态
        qw.eq(Dept.COL_STATUS, Constants.STATUS_TRUE);
        // 排序码
        qw.orderByAsc(Dept.COL_ORDER_NUM);
        return deptMapper.selectList(qw);
    }
}
